package monitor;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

public class MonitorDir extends RunThread{

	public Map preFiles=new HashMap();//存储原来文件路径和最后修改时间
	public Map currentFiles=new HashMap();//存储当前文件路径和最后修改时间
	public Map tmpCurrFiles=new HashMap();
	public Map tmpPreFiles=new HashMap();
	public static Vector vList=new Vector();
	public Vector vtList=new Vector();
	/**
	 * @param second 定时扫描间隔
	 * @param filePath 目录路径
	 * */
	public MonitorDir(int second,String filePath){
		
		super(second,filePath);
		File dirFile=new File(filePath);
		if(dirFile.isDirectory()){
			System.out.println("start to watch: "+dirFile.getAbsolutePath());
		}
	}
	/**获取文件的信息
	 * @param dirPath
	 */
	public void getFilesInfo(String dirPath){
		File dirFile=new File(dirPath);
		preFiles.clear();
		preFiles.putAll(currentFiles);
		currentFiles.clear();
		File[] fileList=dirFile.listFiles();
		for(int i=0;i<fileList.length;i++){
			File tmpFile=fileList[i];
			if(tmpFile.isFile()){//文件
				currentFiles.put(tmpFile.getAbsolutePath(), tmpFile.lastModified());
			}else{//子目录递归
//				String tmpPath=tmpFile.getAbsolutePath();
//				getFilesInfo(tmpPath);
			}
		}
	}
	public void addFile(String file){
		System.out.println(file+" is add");
	}
	public void changed(String file){
		System.out.println(file+" is changed");
	}
	public void delete(String file){
		System.out.println(file+" is delete");
	}
	/**
	 * 监控
	 * */
	@Override
	public void watch(String dirPath) {
		getFilesInfo(dirPath);
        Iterator currentIt =  currentFiles.keySet().iterator();
        while(currentIt.hasNext())
        {
            String filePath = (String)currentIt.next();
            Long currentModify = (Long) currentFiles.get(filePath);
            if(!preFiles.containsKey(filePath))//假如原来的hashmap中不存在当前键，则为增加的
            {
                addFile(filePath);
                //if()
            }
            else if(preFiles.containsKey(filePath))
            {
                Long prevModify = (Long)preFiles.get(filePath);
                if(prevModify.compareTo(currentModify)!=0)//最后修改时间不同，则为改变的
                {
                    changed(filePath);
                }
            }
            //System.out.println("当前------"+filePath+","+currentModify);
        }
        Iterator prevIt = preFiles.keySet().iterator();
        while(prevIt.hasNext())
        {
            String prevFilePath = prevIt.next().toString();
            if(!currentFiles.containsKey(prevFilePath))//原来的键不在当前hashmap中，则为删除的
            {
                delete(prevFilePath);
            }
            //System.out.println("原来------"+prevFilePath+","+tmppreFiles.get(prevFilePath));
        }
	}
	
	 public static void main(String args[])
	    {
	        MonitorDir md = new MonitorDir(5,"D:\\cet4");
	        md.onStart();    
	        
	    }

}















